<%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
	public class TestClass
	{
		public int TestField;
	}
	
	
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        Response.HeaderEncoding = System.Text.Encoding.UTF8;
		Response.Cache.SetCacheability(HttpCacheability.NoCache);
		Response.Cache.SetExpires(DateTime.Now.AddSeconds(-10));
		Response.Cache.SetValidUntilExpires(false);
		Response.Cache.VaryByParams.IgnoreParams = false;
        if (!Page.IsPostBack)
        {
			litVersion.Text = System.Environment.Version.ToString();
			// Test writing permissions
            try
            {
				//XDocument patches = XDocument.Load("http://update.fairportcms.com/configpatches.xml");
                StreamWriter sw = File.CreateText(GetPath() + Session.SessionID + ".txt");
                sw.Close();
                sw.Dispose();
                File.Delete(GetPath() + Session.SessionID + ".txt");
                litWritingPermissions.Text = "ok";
            }
            catch
            {
                litWritingPermissions.Text = "error";
            }
			// Test trust level
            if (GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted)
                litTrustLevel.Text = "ok - " + GetCurrentTrustLevel().ToString();
            else
                litTrustLevel.Text = "error - " + GetCurrentTrustLevel().ToString();
			// Test if reflection is allowed
            try
            {
				Assembly a = Assembly.GetExecutingAssembly();
				foreach (Type t in a.GetTypes())
				{
					if (t.Name == "TestClass")
					{
						TestClass test = (TestClass)(System.Activator.CreateInstance(t));
						litReflection.Text = test != null ? "yes" : "no";
						break;
					}
				}
            }
            catch (Exception ex)
            {
                litReflection.Text = ex.Message;
            }
        }
    }


    private string GetPath()
    {
        string path = Server.MapPath(Request.ApplicationPath);
        if (!path.EndsWith("\\"))
            path += "\\";
        return path;
    }
    
    
    private AspNetHostingPermissionLevel GetCurrentTrustLevel()
    {
        foreach (AspNetHostingPermissionLevel trustLevel in
                new AspNetHostingPermissionLevel[] {
                AspNetHostingPermissionLevel.Unrestricted,
                AspNetHostingPermissionLevel.High,
                AspNetHostingPermissionLevel.Medium,
                AspNetHostingPermissionLevel.Low,
                AspNetHostingPermissionLevel.Minimal 
            })
        {
            try
            {
                new AspNetHostingPermission(trustLevel).Demand();
            }
            catch (System.Security.SecurityException)
            {
                continue;
            }

            return trustLevel;
        }
        return AspNetHostingPermissionLevel.None;
    }    
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">
		.NET version: <asp:Literal ID="litVersion" runat="server" />
		<br />
		File system permissions: <asp:Literal ID="litWritingPermissions" runat="server" />
		<br />
		Trust level: <asp:Literal ID="litTrustLevel" runat="server" />  
		<br />
		Reflection allowed: <asp:literal id="litReflection" runat="server" />
    </form>
</body>
</html>
